package com.ruoyi.shop.service.impl;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.shop.common.core.mapper.ShopLogininforMapper;
import com.ruoyi.shop.member.mapper.ShopMemberMapper;
import com.ruoyi.shop.service.IShopIndexService;
import com.ruoyi.shop.trade.mapper.ShopOrderMapper;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

@Service
public class ShopIndexServiceImpl implements IShopIndexService {
	@Autowired
	private SqlSession sqlSession;
	@Autowired
	private ShopOrderMapper shopOrderMapper;
	@Autowired
	private ShopMemberMapper shopMemberMapper;
	@Autowired
	private ShopLogininforMapper shopLogininforMapper;

	/**
	 * 获取首页展示数据
	 * 
	 * @return
	 */
	@Override
	public JSONObject getIndexData() {
		JSONObject data = JSONUtil.createObj();
		Date currentDate = new Date();
		// 顶部统计
		Map<String, Object> todayTrade = shopOrderMapper.queryTotalTrade(currentDate);
		Map<String, Object> totalTrade = shopOrderMapper.queryTotalTrade(null);// 总销量
		JSONObject total = JSONUtil.createObj();
		// 今日销量
		total.set("todayAmount", todayTrade.get("sales"));
		// 总销量
		total.set("totalAmount", totalTrade.get("sales"));
		// 今日订单数
		total.set("todayCnt", todayTrade.get("cnt"));
		// 总订单数据
		total.set("totalCnt", totalTrade.get("cnt"));
		// 今日访问量
		total.set("todayVisits", shopLogininforMapper.queryLoginCount(currentDate));
		// 总访问量
		total.set("totalVisits", shopLogininforMapper.queryLoginCount(null));
		// 今日会员数
		total.set("todayMembers", shopMemberMapper.queyMemberCount(currentDate));
		// 总会员数
		total.set("totalMembers", shopMemberMapper.queyMemberCount(null));
		data.set("total", total);

		// 版本信息
		JSONObject version = JSONUtil.createObj();
		Properties props = System.getProperties();
		version.set("productName", "favorshop(单体版)");
		version.set("currentVersion", "1.0.0");
		version.set("ruoyiVersion", RuoYiConfig.getVersion());
		version.set("javaVersion", props.getProperty("java.version"));
		version.set("dbVersion", getDbVersion());
		data.set("version", version);

		// 会员购买记录统计
		JSONArray buyArray = JSONUtil.createArray();
		List<Object> titles = new ArrayList<Object>();
		// 0次消费
		JSONObject buyZero = JSONUtil.createObj();
		String zeroTitle = "未消费用户";
		buyZero.set("name", zeroTitle);
		buyZero.set("value", shopOrderMapper.queryBuyCount(0, 0));
		buyArray.set(buyZero);
		titles.add(zeroTitle);
		// 1次消费
		JSONObject buyOne = JSONUtil.createObj();
		String oneTitle = "消费一次用户";
		buyOne.set("name", oneTitle);
		buyOne.set("value", shopOrderMapper.queryBuyCount(1, 1));
		buyArray.set(buyOne);
		titles.add(oneTitle);
		// 2次消费
		JSONObject buyTwo = JSONUtil.createObj();
		String twoTitle = "消费两次用户";
		buyTwo.set("name", twoTitle);
		buyTwo.set("value", shopOrderMapper.queryBuyCount(2, 2));
		buyArray.set(buyTwo);
		titles.add(twoTitle);
		// 多次消费
		JSONObject buyMore = JSONUtil.createObj();
		String moreTitle = "消费多次用户";
		buyMore.set("name", moreTitle);
		buyMore.set("value", shopOrderMapper.queryBuyCount(3, 9999));
		buyArray.set(buyMore);
		titles.add(moreTitle);
		JSONObject buys = JSONUtil.createObj();
		buys.set("titles", titles);
		buys.set("datas", buyArray);
		data.set("buys", buys);

		// 平台交易数据
		List<Map<String, Object>> orderList = shopOrderMapper.queyTradeByMonth();
		List<Object> months = new ArrayList<Object>();
		List<Object> amounts = new ArrayList<Object>();
		List<Object> counts = new ArrayList<Object>();
		for (Map<String, Object> map : orderList) {
			months.add(String.valueOf(map.get("month")).split("-")[1] + "月");
			amounts.add(map.get("tradeAmount"));
			counts.add(map.get("tradeCount"));
		}
		JSONObject orders = JSONUtil.createObj();
		orders.set("months", months);
		orders.set("amounts", amounts);
		orders.set("counts", counts);
		data.set("orders", orders);
		return data;
	}

	/**
	 * 查询登录排行榜
	 * 
	 * @param top
	 * @return
	 */
	public JSONArray selectTopLogin(int top) {
		List<Map<String, Object>> list = shopLogininforMapper.selectLoginTopList(top);
		JSONArray jsonArray = JSONUtil.createArray();
		for (Map<String, Object> map : list) {
			JSONObject jsonObject = JSONUtil.createObj();
			jsonObject.set("loginName", map.get("loginName"));
			jsonObject.set("loginCount", map.get("loginCount"));
			jsonArray.add(jsonObject);
		}
		return jsonArray;
	}

	/**
	 * 数据库版本信息
	 * 
	 * @return
	 */
	private String getDbVersion() {
		Connection conn = null;
		try {
			conn = sqlSession.getConfiguration().getEnvironment().getDataSource().getConnection();
			DatabaseMetaData dbMetaData = conn.getMetaData();
			return dbMetaData.getDatabaseProductVersion();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

}
